package com.engine.jucailinkq.attendance.attendanceanalysis.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.engine.jucailinkq.attendance.enums.AccountingUnitEnum;
import com.engine.jucailinkq.attendance.enums.PersonGroupListTypeEnum;
import com.engine.jucailinkq.attendance.workflow.cmd.GetCqSummarySearchConditionCmd;
import com.engine.jucailinkq.attendance.attendanceanalysis.service.AttendanceSummaryService;
import com.engine.jucailinkq.common.exception.AttendanceRunTimeException;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.core.impl.Service;
import com.engine.kq.biz.KQHolidaySetBiz;
import com.engine.kq.biz.KQReportBiz;
import com.engine.kq.util.ExcelUtil;
import com.engine.kq.util.PageUidFactory;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.job.JobTitlesComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @Author: sy
 * @Description: 出勤汇总实现类
 * @Date: 2024/3/19
 **/
public class AttendanceSummaryServiceImpl extends Service implements AttendanceSummaryService {
    BaseBean bs = new BaseBean();
    /**
     * 新增出勤汇总
     * 基于入参中的出勤汇总主表id,获取主表信息，根据主表中的分部、考勤时间区间等字段，
     * 对出勤结果表、补打卡表、出勤津贴表中数据根据分布和时间区间筛选后，
     * 根据分部、部门、人员id、考勤项目进行分组统计
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> addAttendanceSummary(Map<String, Object> params) {

        bs.writeLog("params : " +  params);
        String mainIds = Util.null2String(params.get("mainIds"));
        bs.writeLog("mainIds : " +  mainIds);
        Thread thread1 = new Thread(() -> {
            List<String> mainIdList = new ArrayList<>();
            if (!mainIds.equals("")) {
                mainIdList = Arrays.asList(mainIds.split(","));
            }
            //更新汇总主表的汇总状态字段为汇总中
            String startUpdateSql = "update uf_jcl_kq_cqhz set hzzt = 1 where id in (" + mainIds + ")";
            DbTools.update(startUpdateSql);
            try {
                //考勤项目
                String kqxmSql = "select * from uf_jcl_kq_kqxm";
                List<Map<String,Object>> kqxmDates = DbTools.getSqlToList(kqxmSql);
                //收集考勤项目id-核算单位映射
                Map<String, String> kqxmUnitInfo = kqxmDates.stream().collect(Collectors.toMap(e->Util.null2String(e.get("id")),e->Util.null2String(e.get("hsdw"))));
                for (String mainId : mainIdList) {
                    List<Map<String, String>> addList = new ArrayList<>();
                    //查询出勤汇总主表信息
                    String sql = "select * from uf_jcl_kq_cqhz where id="+mainId;
                    Map<String,Object> data = DbTools.getSqlToMap(sql);
                    String subCompany = Util.null2String(data.get("fb"));
                    String startDate = Util.null2String(data.get("ksrq"));
                    String endDate = Util.null2String(data.get("jsrq"));
                    bs.writeLog("subCompany : " +  subCompany + ", startDate : " + startDate + ", endDate : " + endDate);
                    if (!subCompany.equals("") && !startDate.equals("") && !endDate.equals("")) {
                        String subCompanyListStr = SubCompanyComInfo.getAllChildSubcompanyId(subCompany, subCompany);
                        //汇总出勤结果表、补打卡表、出勤津贴表数据
                        //出勤结果表
                        List<Map<String, String>> cqAddList = getCqAddList(mainId, subCompanyListStr, startDate, endDate, kqxmUnitInfo);
                        bs.writeLog("cqAddList_size : " +  cqAddList.size());
                        if (cqAddList.size() > 0) {
                            addList.addAll(cqAddList);
                        }
                        //补打卡表
                        List<Map<String, String>> bdkAddList = getBdkAddList(mainId, subCompanyListStr, startDate, endDate);
                        bs.writeLog("bdkAddList_size : " +  bdkAddList.size());
                        if (bdkAddList.size() > 0) {
                            addList.addAll(bdkAddList);
                        }
                        //出勤津贴表
                        List<Map<String, String>> cqjtAddList = getCqjtAddList(mainId, subCompanyListStr, startDate, endDate);
                        bs.writeLog("cqjtAddList_size : " +  cqjtAddList.size());
                        if (cqjtAddList.size() > 0) {
                            addList.addAll(cqjtAddList);
                        }
                        bs.writeLog("addList_size : " +  addList.size());
                        //删除已有汇总数据
                        String delSql = "delete from uf_jcl_kq_cqhz_dt1 where mainid = " + mainId;
                        boolean delSign = DbTools.update(delSql);
                        bs.writeLog("delSign : " +  delSign + ", delSql : " + delSql);
                        //插入汇总数据
                        List<List> dataList = new ArrayList<>();
                        String insertSql = " insert into uf_jcl_kq_cqhz_dt1(mainid,ry,kqxm," +
                                "lc,lf,ls,lt," +
                                "gzfb,gzbm) values(?,?,?,?,?,?,?,?,?)";
                        for (Map<String, String> map : addList) {
                            List<String> list = new ArrayList<>();
                            list.add(mainId);
                            list.add(map.get("ry"));
                            list.add(map.get("kqxm"));
                            list.add(map.get("lc"));
                            list.add(map.get("lf"));
                            list.add(map.get("ls"));
                            list.add(map.get("lt"));
                            list.add("".equals(map.get("gzfb")) ? null : map.get("gzfb"));
                            list.add("".equals(map.get("gzbm")) ? null : map.get("gzbm"));

                            dataList.add(list);
                        }
                        bs.writeLog("dataList_size : " +  dataList.size());
                        if(dataList.size() > 0){
                            bs.writeLog("入库开始---");
                            boolean insertSign =DbTools.updateBatch(insertSql,dataList);
                            bs.writeLog("insertSign : " + insertSign);
                            System.out.println(insertSign);
                        }
                    }
                }
                //更新汇总主表的汇总状态字段为汇总中
                String endUpdateSql = "update uf_jcl_kq_cqhz set hzzt = 2 where id in (" + mainIds + ")";
                DbTools.update(endUpdateSql);
            } catch (Exception e) {
                e.printStackTrace();
                //更新汇总主表的汇总状态字段为未汇总
                String endUpdateSql = "update uf_jcl_kq_cqhz set hzzt = 0 where id in (" + mainIds + ")";
                DbTools.update(endUpdateSql);
            }
        });
        thread1.start();
        return new HashMap<>();
    }

    @Override
    public Map<String, Object> getCqSummaryReport(Map<String, Object> params) {
        Map<String,Object> retmap = new HashMap<String,Object>();
        try{
            String pageUid = PageUidFactory.getHrmPageUid("CqSummaryReport");

            SubCompanyComInfo subCompanyComInfo = new SubCompanyComInfo();
            DepartmentComInfo departmentComInfo = new DepartmentComInfo();
            ResourceComInfo resourceComInfo = new ResourceComInfo();
            JobTitlesComInfo jobTitlesComInfo = new JobTitlesComInfo();

            JSONObject jsonObj = JSON.parseObject(Util.null2String(params.get("data")));
            String attendanceSerial = Util.null2String(jsonObj.get("attendanceSerial"));
            String fromDate = Util.null2String(jsonObj.get("fromDate"));
            String toDate = Util.null2String(jsonObj.get("toDate"));
            String typeSelect =Util.null2String(jsonObj.get("typeselect"));
            if(typeSelect.length()==0) {typeSelect = "3";}
            if(!typeSelect.equals("") && !typeSelect.equals("0")&& !typeSelect.equals("6")){
                if(typeSelect.equals("1")){
                    fromDate = TimeUtil.getCurrentDateString();
                    toDate = TimeUtil.getCurrentDateString();
                }else{
                    fromDate = TimeUtil.getDateByOption(typeSelect,"0");
                    toDate = TimeUtil.getDateByOption(typeSelect,"1");
                }
            }
            //人员状态
            String status = Util.null2String(jsonObj.get("status"));
            String subCompanyId = Util.null2String(jsonObj.get("subCompanyId"));
            String departmentId = Util.null2String(jsonObj.get("departmentId"));
            String resourceId = Util.null2String(jsonObj.get("resourceId"));
            bs.writeLog("status : " + status + ", subCompanyId : " + subCompanyId + ", departmentId : " + departmentId + ", resourceId : " + resourceId);
            String allLevel = Util.null2String(jsonObj.get("allLevel"));
            String isNoAccount = Util.null2String(jsonObj.get("isNoAccount"));
            String viewScope = Util.null2String(jsonObj.get("viewScope"));
            String isFromMyAttendance = Util.null2String(jsonObj.get("isFromMyAttendance"));//是否是来自我的考勤的请求，如果是，不加载考勤报表权限共享的限制，不然我的考勤会提示无权限
            int pageIndex = Util.getIntValue(Util.null2String(jsonObj.get("pageIndex")), 1);
            int pageSize =  KQReportBiz.getPageSize(Util.null2String(jsonObj.get("pageSize")),pageUid,user.getUID());
            int count = 0;
            int pageCount = 0;
            int isHavePre = 0;
            int isHaveNext = 0;
            //设置列头
            List<Object> columns = new ArrayList();
            Map<String,Object> column = null;
            //人员相关信息
            column = new HashMap<>();
            column.put("title", SystemEnv.getHtmlLabelName(413, user.getLanguage())); //姓名
            column.put("unit", "");
            column.put("dataIndex", "lastname");
            column.put("type", "lastname");
            column.put("key", "lastname");
            column.put("rowSpan", 3);
            column.put("width", 105);
            column.put("showDetial", "1");
            columns.add(column);

            column = new HashMap<>();
            column.put("title", SystemEnv.getHtmlLabelName(528842, user.getLanguage())); //分部
            column.put("unit", "");
            column.put("dataIndex", "subcompany");
            column.put("type", "subcompany");
            column.put("key", "subcompany");
            column.put("rowSpan", 3);
            column.put("width", 105);
            column.put("showDetial", "1");
            columns.add(column);

            column = new HashMap<>();
            column.put("title", SystemEnv.getHtmlLabelName(528843, user.getLanguage())); //部门
            column.put("unit", "");
            column.put("dataIndex", "department");
            column.put("type", "department");
            column.put("key", "department");
            column.put("rowSpan", 3);
            column.put("width", 105);
            column.put("showDetial", "1");
            columns.add(column);
            //考勤项目
            String kqxmSql = "select * from uf_jcl_kq_kqxm";
            List<Map<String,Object>> kqxmDates = DbTools.getSqlToList(kqxmSql);
            for(Map<String,Object> kqxm : kqxmDates) {
                String unit = getKqxmUnit(Util.null2String(kqxm.get("hsdw")));
                column = new HashMap<>();
                column.put("title", kqxm.get("mc"));
                column.put("unit", unit);
                column.put("dataIndex", "kqxm" + "_" + kqxm.get("id"));
                column.put("type", "kqxm" + "_" + kqxm.get("id"));
                column.put("key", "kqxm" + "_" + kqxm.get("id"));
                column.put("rowSpan", 3);
                column.put("width", 105);
                column.put("showDetial", "1");
                columns.add(column);
            }
            bs.writeLog("columns : " + columns);
            //收集考勤项目id-核算单位映射
            Map<String, String> kqxmUnitInfo = kqxmDates.stream().collect(Collectors.toMap(e->Util.null2String(e.get("id")),e->Util.null2String(e.get("hsdw"))));
            //获取列表结果，typeSelect为9、10时查询出勤汇总明细表数据，其他则生成临时汇总数据
            List<Map<String,Object>> dtDates = null;
            LocalDate targetDate = LocalDate.now(); // 获取当前日期
            bs.writeLog("currentDate : " + targetDate);
            if (typeSelect.equals("9")) {
                String targetYear = String.valueOf(targetDate.getYear());
                String targetMonth = String.valueOf(targetDate.getMonthValue());
                bs.writeLog("targetYear : " + targetYear + ", targetMonth : " + targetMonth);
                dtDates = getDtDates(targetYear, targetMonth, status, subCompanyId, departmentId, resourceId);
            } else if (typeSelect.equals("10")) {
                targetDate = targetDate.minusMonths(1);
                String targetYear = String.valueOf(targetDate.getYear());
                String targetMonth = String.valueOf(targetDate.getMonthValue());
                bs.writeLog("targetYear : " + targetYear + ", targetMonth : " + targetMonth);
                dtDates = getDtDates(targetYear, targetMonth, status, subCompanyId, departmentId, resourceId);
            } else {
                //调用临时汇总数据方法，生成数据
                jsonObj.put("fromDate", fromDate);
                jsonObj.put("toDate", toDate);
                dtDates = getTemAttendanceSummary(jsonObj, kqxmUnitInfo);
            }
            bs.writeLog("dtDates_size : " +  (dtDates==null ? "null" : dtDates.size()));
            //处理明细表数据，分组生成列表数据
            Map<String, Map<String, String>> groupData = new HashMap<>();
            for (Map<String, Object> map : dtDates) {
                String groupKey = map.get("ry") + "_" + map.get("gzfb") + "_" + map.get("gzbm");
                Map<String, String> itemMap = new HashMap<>();
                if (groupData.get(groupKey) == null) {
                    //尚未存在则新建，加入
                    itemMap.put("resourceId", Util.null2String(map.get("ry")));
                    itemMap.put("lastname", resourceComInfo.getLastname(Util.null2String(map.get("ry"))));
                    itemMap.put("subcompanyId", Util.null2String(map.get("gzfb")));
                    itemMap.put("subcompany", subCompanyComInfo.getSubcompanyname(Util.null2String(map.get("gzfb"))));
                    itemMap.put("departmentId", Util.null2String(map.get("gzbm")));
                    itemMap.put("department", departmentComInfo.getDepartmentName(Util.null2String(map.get("gzbm"))));
                    //设置考勤项目累计数值，累天、累时、累分最多有一个有值，都没有则取累次
                    String cumulativeValue = Util.null2String(map.get("lt")) + Util.null2String(map.get("ls")) + Util.null2String(map.get("lf"));
                    if (!cumulativeValue.equals("")) {
                        itemMap.put("kqxm" + "_" + map.get("kqxm").toString(), cumulativeValue);
                    } else {
                        itemMap.put("kqxm" + "_" + map.get("kqxm").toString(), map.get("lc").toString());
                    }
                    groupData.put(groupKey, itemMap);
                } else {
                    //已存在则新增元素
                    itemMap = groupData.get(groupKey);
                    //设置考勤项目累计数值，累天、累时、累分最多有一个有值，都没有则取累次
                    String cumulativeValue = Util.null2String(map.get("lt")) + Util.null2String(map.get("ls")) + Util.null2String(map.get("lf"));
                    if (!cumulativeValue.equals("")) {
                        itemMap.put("kqxm" + "_" + map.get("kqxm").toString(), cumulativeValue);
                    } else {
                        itemMap.put("kqxm" + "_" + map.get("kqxm").toString(), map.get("lc").toString());
                    }
                    groupData.put(groupKey, itemMap);
                }
            }
            //获取最终的list，根据人员id排序
            List<Map<String, String>> dataList = new ArrayList<>(groupData.values());
            Collections.sort(dataList, new Comparator<Map<String, String>>() {
                @Override
                public int compare(Map<String, String> o1, Map<String, String> o2) {
                    String str1 = o1.get("resourceId").toString();
                    String str2 = o2.get("resourceId").toString();
                    //str1在前，str2在后，默认升序
                    return str1.compareTo(str2);

                }
            });
            String exportSign = Util.null2String(params.get("exportSign"));
            if (exportSign.equals("1")) {
                //导出时，不做分页处理
                retmap.put("columns",columns);
                retmap.put("datas",dataList);
            } else {
                //分页处理
                int start = (pageIndex - 1) * pageSize;
                int end = Math.min(pageIndex * pageSize, dataList.size());
                List<Map<String, String>> resultList = new ArrayList<>();
                if (start <= dataList.size()) {
                    resultList = dataList.subList(start, end);
                }
                count = dataList.size();
                if (count <= 0) {
                    pageCount = 0;
                } else {
                    pageCount = count / pageSize + ((count % pageSize > 0) ? 1 : 0);
                }
                isHaveNext = (pageIndex <= pageCount) ? 1 : 0;
                isHavePre = (pageIndex >= 1) ? 1 : 0;

                List<Object> lsHolidays = KQHolidaySetBiz.getHolidaySetListByScope(""+user.getUID(),fromDate,toDate);
                retmap.put("holidays", lsHolidays);

                retmap.put("columns",columns);
                retmap.put("datas",resultList);
                retmap.put("pagesize", pageSize);
                retmap.put("pageindex", pageIndex);
                retmap.put("count", count);
                retmap.put("pagecount", pageCount);
                retmap.put("ishavepre", isHavePre);
                retmap.put("ishavenext", isHaveNext);
            }

        }catch (Exception e){
            bs.writeLog(e);
            retmap.put("errorMessage", e.getMessage());
            return retmap;
        }
        return retmap;
    }

    @Override
    public Map<String, Object> getCqSummarySearchCondition(Map<String, Object> params) {
        return this.commandExecutor.execute(new GetCqSummarySearchConditionCmd(params, user));
    }

    @Override
    public Map<String, Object> exportCqSummaryReport(Map<String, Object> params, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> retmap = null;
        try {
            retmap = new HashMap<String, Object>();
            params.put("exportSign", "1");
            Map<String, Object> cqSummaryReportResult = getCqSummaryReport(params);
            List<Map<String, String>> resultList = (List<Map<String, String>>) cqSummaryReportResult.get("datas");
            List<Map<String, Object>> columns = (List<Map<String, Object>>) cqSummaryReportResult.get("columns");

            JSONObject jsonObj = JSON.parseObject(Util.null2String(params.get("data")));
            String showColumns = Util.null2String(jsonObj.get("showColumns"));
            List<String> showColumnKeyList = new ArrayList<>();
            showColumnKeyList.add("lastname");
            if (!showColumns.equals("")) {
                showColumnKeyList.addAll(Arrays.asList(showColumns.split(",")));
            }

            //组装导出数据
            List<List<Object>> dataList = new ArrayList<>();
            List<Object> data = null;
            for (Map<String, String> dataItem : resultList) {
                data = new ArrayList<>();
                for (String columnKey : showColumnKeyList) {
                    if (columnKey.equals("subcompany") || columnKey.equals("department")) {
                        data.add(Util.formatMultiLang(Util.null2String(dataItem.get(columnKey)), String.valueOf(user.getLanguage())));
                    } else {
                        data.add(Util.null2String(dataItem.get(columnKey)));
                    }
                }
                dataList.add(data);
            }
            //获取列头key和value映射
            Map<String, String> columnValueMap = new HashMap<>();
            for(Map<String, Object> columnItem : columns) {
                String title = Util.null2String(columnItem.get("title"));
                String unit = Util.null2String(columnItem.get("unit"));
                if (!"".equals(title) && !"".equals(unit)) {
                    title = title + "(" + unit + ")";
                }
                columnValueMap.put(columnItem.get("key").toString(), title);
            }
            //组装导出列头
            List<Object> titleList = new ArrayList<>();
            Map<String,Object> title = null;
            for (String columnKey : showColumnKeyList) {
                title = new HashMap<>();
                title.put("title", columnValueMap.get(columnKey));
                title.put("width", 30*256);
                titleList.add(title);
            }
            //组装页标签
            Map<String, Object> sheet = new HashMap<>();
            sheet.put("sheetName", SystemEnv.getHtmlLabelName(390351, user.getLanguage()));
            sheet.put("sheetTitle", SystemEnv.getHtmlLabelName(390351, user.getLanguage()));
            sheet.put("dataList", dataList);
            sheet.put("titleList", titleList);
            sheet.put("createFile", "1");
            List<Object> lsSheet = new ArrayList<>();
            lsSheet.add(sheet);

            LinkedHashMap<String, Object> workbook = new LinkedHashMap<>();
            workbook.put("sheet", lsSheet);
            workbook.put("fileName", SystemEnv.getHtmlLabelName(390351, user.getLanguage()));
            ExcelUtil ExcelUtil = new ExcelUtil();
            Map<String,Object> exportMap= ExcelUtil.export(workbook,request,response);
            bs.writeLog("考勤汇总报表导出：");
            bs.writeLog(Util.null2String(exportMap.get("url")));
            retmap.putAll(exportMap);
            retmap.put("status", "1");
        } catch (Exception e) {
            retmap.put("status", "-1");
            retmap.put("message", SystemEnv.getHtmlLabelName(382661, user.getLanguage()));
            bs.writeLog(e.getMessage());
        }
        return retmap;
    }

    @Override
    public Map<String, Object> refreshViewAttendanceDaily() {
        Map<String, Object> retmap = new HashMap<>();
        String sql = "";
        String viewName = "v_attdaily";
        //判断视图v_attdaily是否存在
        RecordSet rs = new RecordSet();
        String dbType = rs.getDBType();
        if ("mysql".equals(dbType) || "postgresql".equals(dbType) || "sqlserver".equals(dbType) || "dm".equals(dbType) || "st".equals(dbType)) {
            sql = "SELECT * FROM information_schema.views WHERE table_name = '" + viewName + "'";
        } else if ("oracle".equals(dbType)) {
            sql = "SELECT * FROM ALL_VIEWS WHERE VIEW_NAME = '" + viewName + "'";
        } else if ("jc".equals(dbType)) {
            sql = "SELECT * FROM sys.views WHERE viewname = '" + viewName + "'";
        } else {
            retmap.put("status", "-1");
            retmap.put("message", "不支持的数据库类型，无法查询试图是否存在！");
            return retmap;
        }
        Map<String, Object> viewInfo = DbTools.getSqlToMap(sql);
        //存在则删除
        if (viewInfo.size() > 0) {
            sql = "DROP VIEW " + viewName;
            boolean dropSign = DbTools.update(sql);
            bs.writeLog("删除视图结果：" + dropSign);
        }

        //收集考勤项目
        String kqxmSql = "select * from uf_jcl_kq_kqxm";
        List<Map<String, Object>> kqxmList = DbTools.getSqlToList(kqxmSql);
        //收集视图列头
        StringBuilder viewColumn = new StringBuilder("id,ygid,rq,fbid,bm,zw,bc");
        //拼接视图内容sql
        StringBuilder viewResourceSql = new StringBuilder("SELECT id, ygid, rq, fbid, bm, zw, bc");
        //考勤项目核算单位、id、名称
        String hsdw = "";
        String kqxmId = "";
        String mc = "";
        for (Map<String, Object> kqxmInfo : kqxmList) {
            hsdw = Util.null2String(kqxmInfo.get("hsdw"));
            kqxmId = Util.null2String(kqxmInfo.get("id"));
            mc = Util.null2String(kqxmInfo.get("mc"));
            int lengthCompareValue = "字".getBytes().length * 9;
            int mcLength = mc.getBytes().length;
            if ("oracle".equals(dbType) && mcLength > lengthCompareValue) {
                mc = mc.substring(0,9);
            }
            if ("0".equals(hsdw)) {
                viewResourceSql.append(", SUM(CASE WHEN xm = ").append(kqxmId).append(" THEN sc ELSE 0 END) AS \"").append(mc).append("_t\"");
                viewResourceSql.append(", SUM(CASE WHEN xm = ").append(kqxmId).append(" THEN 1 ELSE 0 END) AS \"").append(mc).append("_c\"");
                viewColumn.append(",").append(mc).append("_t").append(",").append(mc).append("_c");
            } else if ("1".equals(hsdw)) {
                if ("1".equals(kqxmId)) {
                    viewResourceSql.append(", SUM(CASE WHEN xm = 1 OR xm = 5").append(" THEN sc ELSE 0 END) AS \"").append(mc).append("_s\"");
                } else {
                    viewResourceSql.append(", SUM(CASE WHEN xm = ").append(kqxmId).append(" THEN sc ELSE 0 END) AS \"").append(mc).append("_s\"");
                }
                viewResourceSql.append(", SUM(CASE WHEN xm = ").append(kqxmId).append(" THEN 1 ELSE 0 END) AS \"").append(mc).append("_c\"");
                viewColumn.append(",").append(mc).append("_s").append(",").append(mc).append("_c");
            } else if ("2".equals(hsdw)) {
                viewResourceSql.append(", SUM(CASE WHEN xm = ").append(kqxmId).append(" THEN sc ELSE 0 END) AS \"").append(mc).append("_f\"");
                viewResourceSql.append(", SUM(CASE WHEN xm = ").append(kqxmId).append(" THEN 1 ELSE 0 END) AS \"").append(mc).append("_c\"");
                viewColumn.append(",").append(mc).append("_f").append(",").append(mc).append("_c");
            } else {
                viewResourceSql.append(", SUM(CASE WHEN xm = ").append(kqxmId).append(" THEN 1 ELSE 0 END) AS \"").append(mc).append("_c\"");
                viewColumn.append(",").append(mc).append("_c");
            }
        }
        //ifnull函数的适配
        String ifNullFunction = "";
        if("mysql".equals(dbType) || "dm".equals(dbType) || "jc".equals(dbType) || "st".equals(dbType)) {
            ifNullFunction = "IFNULL";
        } else if ("oracle".equals(dbType)) {
            ifNullFunction = "NVL";
        } else if ("sqlserver".equals(dbType)) {
            ifNullFunction = "ISNULL";
        } else if ("postgresql".equals(dbType)) {
            ifNullFunction = "COALESCE";
        }
        //创建新的视图
        String createViewSql = "CREATE VIEW " + viewName;
        createViewSql = createViewSql + " AS " + viewResourceSql.toString();
        createViewSql = createViewSql + " FROM (" +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, xm1 AS xm," + ifNullFunction + "(sc1,0) AS sc FROM uf_jcl_kq_cqjg WHERE (xm1 IS NOT NULL) UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, xm2," + ifNullFunction + "(sc2,0) FROM uf_jcl_kq_cqjg WHERE (xm2 IS NOT NULL) UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, xm3," + ifNullFunction + "(sc3,0) FROM uf_jcl_kq_cqjg WHERE (xm3 IS NOT NULL) UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, xm4," + ifNullFunction + "(sc4,0) FROM uf_jcl_kq_cqjg WHERE (xm4 IS NOT NULL) UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, xm5," + ifNullFunction + "(sc5,0) FROM uf_jcl_kq_cqjg WHERE (xm5 IS NOT NULL) UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, xm6," + ifNullFunction + "(sc6,0) FROM uf_jcl_kq_cqjg WHERE (xm6 IS NOT NULL) UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, xm7," + ifNullFunction + "(sc7,0) FROM uf_jcl_kq_cqjg WHERE (xm7 IS NOT NULL) UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, xm8," + ifNullFunction + "(sc8,0) FROM uf_jcl_kq_cqjg WHERE (xm8 IS NOT NULL) UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, CASE WHEN cqzt = 0 THEN '1' ELSE '5' END, cqsc FROM uf_jcl_kq_cqjg UNION ALL "
                +
                "SELECT id, fbid, bm, zw, ygid, rq, bc, CASE WHEN rqlx = 0 OR rqlx = 4 THEN '2' WHEN rqlx = 2 OR rqlx = 3 THEN '3' WHEN rqlx = 1 THEN '4' ELSE '0' END ,cqsc FROM uf_jcl_kq_cqjg UNION ALL "
                +
                "SELECT T2.id, T2.fbid, T2.bm, T2.zw, T2.ygid, T2.rq, T2.bc, T1.jtlx, " + ifNullFunction + "(T1.sc,0) FROM uf_jcl_kq_cqjt T1 LEFT JOIN uf_jcl_kq_cqjg T2 ON T1.yg = T2.ygid AND T1.rq = T2.rq WHERE T1.zt = 1 UNION ALL "
                +
                "SELECT T2.id, T2.fbid, T2.bm, T2.zw, T2.ygid, T2.rq, T2.bc, T1.bdklx, 1 FROM uf_jcl_kq_bdkjl T1 LEFT JOIN uf_jcl_kq_cqjg T2 ON T1.bdkry = T2.ygid AND T1.dkrq = T2.rq WHERE T1.jlzt = 1 AND T2.id IS NOT NULL "
                +
                ") ta GROUP BY id, ygid, rq, fbid, bm, zw, bc";

        boolean createSign = DbTools.update(createViewSql);
        bs.writeLog("创建视图结果：" + createSign);
        if (createSign) {
            retmap.put("status", "1");
            retmap.put("message", "考勤日报相关视图刷新成功！");
        } else {
            retmap.put("status", "-1");
            retmap.put("message", "考勤日报相关视图刷新失败！");
            bs.writeLog("创建视图sql：[===" + createViewSql + "===]");
        }
        return retmap;

    }

    private String getKqxmUnit(String hsdw) {
        if (hsdw.equals("0")) {
            return SystemEnv.getHtmlLabelName(518514, user.getLanguage()); //天
        } else if (hsdw.equals("1")) {
            return SystemEnv.getHtmlLabelName(518515, user.getLanguage()); //小时
        } else if (hsdw.equals("2")) {
            return SystemEnv.getHtmlLabelName(518516, user.getLanguage()); //分钟
        } else if (hsdw.equals("3")) {
            return SystemEnv.getHtmlLabelName(18083, user.getLanguage()); //次
        } else {
            return "";
        }
    }

    private List<Map<String,Object>> getDtDates(String targetYear, String targetMonth, String status, String subCompanyIds, String depIds, String empIds) {
        //20240528需求变更,
        // 当前登录人员为人员分组管理员时，取其管理的人员明细（人员id,生效日期，失效日期）来筛选待汇总的数据；
        // 当前登录人员非人员分组管理员时，取其下属的人员id来筛选待汇总的数据
        Map<String, Object> userManageInfo = getUserManageInfo();
        List<String> targetEmpIdList = new ArrayList<>();
        if (!"".equals(empIds)) {
            targetEmpIdList = getManageEmpIds(userManageInfo, Arrays.asList(empIds.split(",")), targetYear + "-" + targetMonth);
        } else {
            targetEmpIdList = getManageEmpIds(userManageInfo, new ArrayList<>(), targetYear + "-" + targetMonth);
        }
        if (targetEmpIdList.size() == 0) {
            return new ArrayList<>();
        }
        String mainListSql = "select * from uf_jcl_kq_cqhz where nd= " + targetYear + " and yf = " + targetMonth;
        List<Map<String,Object>> mainDates = DbTools.getSqlToList(mainListSql);
        List<String> mainIds = new ArrayList<>();
        for (Map<String, Object> map : mainDates) {
            mainIds.add(map.get("id").toString());
        }
        bs.writeLog("mainIds : " + mainIds);
        String dtListSql = "select * from uf_jcl_kq_cqhz_dt1 a left join hrmresource b on a.ry = b.id where 1=1";
        if(status.length()>0){
            if (!status.equals("8") && !status.equals("9")) {
                dtListSql += " and b.status = "+status+ "";
            }else if (status.equals("8")) {
                dtListSql += " and (b.status = 0 or b.status = 1 or b.status = 2 or b.status = 3) ";
            }
        }
        if (!subCompanyIds.equals("")) {
            dtListSql = dtListSql + " and a.gzfb in (" + subCompanyIds + ")";
        }
        if (!depIds.equals("")) {
            dtListSql = dtListSql + " and a.gzbm in (" + depIds + ")";
        }
        if (targetEmpIdList.size() > 0) {
//            dtListSql = dtListSql + " and a.ry in (" + empIds + ")";
            dtListSql = dtListSql + " and a.ry in (" + String.join(",",targetEmpIdList) + ")";
        }
        if (mainIds.size() > 0) {
            dtListSql = dtListSql + " and a.mainid in (" + String.join(",", mainIds) + ")";
            List<Map<String,Object>> dtDates = DbTools.getSqlToList(dtListSql);
            bs.writeLog("dtDates_size : " + dtDates.size() + ", dtListSql : " + dtListSql);
            return dtDates;
        } else {
            return new ArrayList<>();
        }

    }

    private List<String> getManageEmpIds(Map<String, Object> userManageInfo, List<String> empIdList, String yearMonth) {
        List<String> manageEmpIdList = new ArrayList<>();
        manageEmpIdList.add(String.valueOf(user.getUID()));
        boolean groupManageSign = (boolean) userManageInfo.get("groupManageSign");
        if (groupManageSign) {
            userManageInfo.remove("groupManageSign");
            String listType = (String) userManageInfo.get("listType");
            if (PersonGroupListTypeEnum.PERSON.getKey().equals(listType)) {
                userManageInfo.remove("listType");
                userManageInfo.forEach((key, value) -> {
                    Map<String, Object> forceTimeItem = (Map<String, Object>) value;
                    String startDate = (String) forceTimeItem.get("startDate");
                    String endDate = (String) forceTimeItem.get("endDate");
                    if (startDate.substring(0,7).compareTo(yearMonth) <= 0 && ("".equals(endDate) || endDate.substring(0,7).compareTo(yearMonth) >= 0)) {
                        manageEmpIdList.add(key);
                    }

                });
            } else if (PersonGroupListTypeEnum.CONDITION.getKey().equals(listType)) {
                userManageInfo.remove("listType");
                userManageInfo.forEach((key, value) -> {
                    List<Map<String, Object>> forceTimeItemList = (List<Map<String, Object>>) value;
                    boolean forceTimeAllowSign;
                    for (Map<String, Object> forceTimeItem : forceTimeItemList) {
                        String startDate = (String) forceTimeItem.get("startDate");
                        String endDate = (String) forceTimeItem.get("endDate");
                        forceTimeAllowSign = startDate.substring(0,7).compareTo(yearMonth) <= 0 && ("".equals(endDate) || endDate.substring(0,7).compareTo(yearMonth) >= 0);
                        if (forceTimeAllowSign) {
                            manageEmpIdList.add(key);
                            break;
                        }
                    }
                });

            }
        } else {
            List<String> subEmpIdList = (List<String>) userManageInfo.get("subEmpIdList");
            manageEmpIdList.addAll(subEmpIdList);
        }
        if (empIdList.size() > 0) {
            empIdList = empIdList.stream()
                    .filter(manageEmpIdList::contains)
                    .distinct()
                    .collect(Collectors.toList());
        } else {
            empIdList = manageEmpIdList.stream().distinct().collect(Collectors.toList());
        }
        return empIdList;
    }

    /**
     * 获取临时出勤汇总数据
     * 基于入参中分部、部门、人员id、人员状态、考勤时间区间等字段，
     * 对出勤结果表、补打卡表、出勤津贴表中数据根据分布和时间区间筛选后，
     * 根据分部、部门、人员id、考勤项目进行分组统计
     * @param params
     * @return
     */
    public List<Map<String, Object>> getTemAttendanceSummary(Map<String, Object> params, Map<String, String> kqxmUnitInfo) {

        bs.writeLog("params : " + params);
        List<Map<String, Object>> addList = new ArrayList<>();
        //处理筛选条件
        //时间范围，今天-1，本周-2，本月-3，本季-4，本年-5，指定日期范围-6，上个月-7，上一年-8
        String dateRange = Util.null2String(params.get("typeselect"));//时间范围
        String startDate = Util.null2String(params.get("fromDate"));
        String endDate = Util.null2String(params.get("toDate"));
        if (dateRange.length() == 0) {
            dateRange = "3";
        }
        if (!dateRange.equals("") && !dateRange.equals("0") && !dateRange.equals("6")) {
            if (dateRange.equals("1")) {
                startDate = TimeUtil.getCurrentDateString();
                endDate = TimeUtil.getCurrentDateString();
            } else {
                startDate = TimeUtil.getDateByOption(dateRange, "0");
                endDate = TimeUtil.getDateByOption(dateRange, "1");
            }
        }
        //查看范围，总部-0，分部-1，部门-2，人员-3
        String viewRange = Util.null2String(params.get("viewScope"));//查看范围
        String subCompanyIds = Util.null2String(params.get("subCompanyId"));//可以多个分部id，以,分隔
        String departmentIds = Util.null2String(params.get("departmentId"));//可以多个部门id，以,分隔
        String empIds = Util.null2String(params.get("resourceId"));//可以多个人员id，以,分隔
        //人员状态，试用-0，正式-1，临时-2，试用延期-3，解聘-4，离职-5，退休-6，无效-7，在职-8，全部-9
        String empStatus = Util.null2String(params.get("status"));//人员状态
        bs.writeLog("startDate : " + startDate + ", endDate : " + endDate + ", subCompanyIds : " +  subCompanyIds +  ", departmentIds : " + departmentIds + ", empIds : " + empIds + ", empStatus : " + empStatus);
        //20240528需求变更,
        // 当前登录人员为人员分组管理员时，取其管理的人员明细（人员id,生效日期，失效日期）来筛选待汇总的数据；
        // 当前登录人员非人员分组管理员时，取其下属的人员id来筛选待汇总的数据
        Map<String, Object> userManageInfo = getUserManageInfo();
        if (!startDate.equals("") && !endDate.equals("")) {
            //临时汇总出勤结果表、补打卡表、出勤津贴表数据
            //出勤结果表
            List<Map<String, Object>> cqAddList = getTemCqAddList(startDate, endDate, subCompanyIds, departmentIds, empIds, empStatus, userManageInfo, kqxmUnitInfo);
            bs.writeLog("cqAddList_size : " +  cqAddList.size());
            if (cqAddList.size() > 0) {
                addList.addAll(cqAddList);
            }
            //补打卡表
            List<Map<String, Object>> bdkAddList = getTemBdkAddList(startDate, endDate, subCompanyIds, departmentIds, empIds, empStatus, userManageInfo);
            bs.writeLog("bdkAddList_size : " +  bdkAddList.size());
            if (bdkAddList.size() > 0) {
                addList.addAll(bdkAddList);
            }
            //出勤津贴表
            List<Map<String, Object>> cqjtAddList = getTemCqjtAddList(startDate, endDate, subCompanyIds, departmentIds, empIds, empStatus, userManageInfo);
            bs.writeLog("cqjtAddList_size : " +  cqjtAddList.size());
            if (cqjtAddList.size() > 0) {
                addList.addAll(cqjtAddList);
            }

        }
        return addList;
    }

    //获取当前登录者所能管理的人员信息，
    // 如果其是分组管理员，则从分组明细中获取被管理人员的人员id和管理时间区间信息；
    // 否则只获取将当前登录人员作为直接上级的人员信息
    private Map<String, Object> getUserManageInfo() {
        Map<String, Object> userManageInfo = new HashMap<>();
        String sql = "select * from uf_ryqz where fzgly = " + user.getUID() +  " and (delete_type=0 or delete_type is null)";
        List<Map<String, Object>> data = DbTools.getSqlToList(sql);
        if (data.size() == 1) {
            userManageInfo.put("groupManageSign", true);
            Map<String, Object> empGroupUserInfo = CommonUtil.getEmpGroupUserInfo(data.get(0).get("id").toString());
            userManageInfo.putAll(empGroupUserInfo);
        } else if (data.size() > 1) {
            throw new AttendanceRunTimeException("当前登录人：" + user.getLastname() + "不可以同时管理一个以上的人员分组！");
        } else {
            userManageInfo.put("groupManageSign", false);
            String subEmpSql = "select * from hrmresource where managerid = " + user.getUID();
            List<Map<String, Object>> subEmpData = DbTools.getSqlToList(subEmpSql);
            List<String> subEmpIdList = new ArrayList<>();
            for (Map<String, Object> map : subEmpData) {
                subEmpIdList.add(map.get("id").toString());
            }
            userManageInfo.put("subEmpIdList", subEmpIdList);
        }
        return userManageInfo;
    }

    //汇总出勤结果表
    public List<Map<String, String>> getCqAddList(String mainId, String subCompanyListStr, String startDate, String endDate, Map<String, String> kqxmUnitInfo) {
        String cqSql = "select * from uf_jcl_kq_cqjg where fbid in (" + subCompanyListStr + ") and rq >= '" + startDate +"'" + " and rq <= '" + endDate +"'";
        List<Map<String,Object>> cqDates = DbTools.getSqlToList(cqSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealCqDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : cqDates) {
            //出勤状态
            String cqzt = Util.null2String(map.get("cqzt"));
            if (!"".equals(cqzt)) {
                //匹配出勤状态对应的考勤项目id
                String kqxmId = "0".equals(cqzt) ? "1" : "5";
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + kqxmId;
                Map<String,Object> itemMap = new HashMap<>();
                if ("1".equals(kqxmId) && Util.null2String(kqxmUnitInfo.get("1")).equals(AccountingUnitEnum.HOUR.getKey())) {
                    itemMap.put("fbid", map.get("fbid"));
                    itemMap.put("bm", map.get("bm"));
                    itemMap.put("ygid", map.get("ygid"));
                    itemMap.put("xm", kqxmId);
                    itemMap.put("sc", Util.null2String(map.get("cqsc")));
                } else {
                    itemMap.put("fbid", map.get("fbid"));
                    itemMap.put("bm", map.get("bm"));
                    itemMap.put("ygid", map.get("ygid"));
                    itemMap.put("xm", kqxmId);
                    itemMap.put("sc", "1");
                }

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }
            //日期类型
            String rqlx = Util.null2String(map.get("rqlx"));
            if (!rqlx.equals("")) {
                //匹配日期类型对应的考勤项目id
                String kqxmId = null;
                if (rqlx.equals("0") || rqlx.equals("4")) {
                    kqxmId = "2";
                } else if (rqlx.equals("2") || rqlx.equals("3")) {
                    kqxmId = "3";
                } else if (rqlx.equals("1")) {
                    kqxmId = "4";
                }

                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + kqxmId;
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", kqxmId);
                itemMap.put("sc", "1");

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }
            //项目1
            if (!Util.null2String(map.get("xm1")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm1");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm1"));
                itemMap.put("sc", map.get("sc1"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目2
            if (!Util.null2String(map.get("xm2")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm2");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm2"));
                itemMap.put("sc", map.get("sc2"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目3
            if (!Util.null2String(map.get("xm3")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm3");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm3"));
                itemMap.put("sc", map.get("sc3"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目4
            if (!Util.null2String(map.get("xm4")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm4");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm4"));
                itemMap.put("sc", map.get("sc4"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目5
            if (!Util.null2String(map.get("xm5")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm5");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm5"));
                itemMap.put("sc", map.get("sc5"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目6
            if (!Util.null2String(map.get("xm6")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm6");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm6"));
                itemMap.put("sc", map.get("sc6"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目7
            if (!Util.null2String(map.get("xm7")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm7");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm7"));
                itemMap.put("sc", map.get("sc7"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目8
            if (!Util.null2String(map.get("xm8")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm8");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm8"));
                itemMap.put("sc", map.get("sc8"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }

        }
        //获取考勤项目和核算单位的映射
        Map<String, String> hsdwInfoMap = new HashMap<>();
        String kqxmSql = "select * from uf_jcl_kq_kqxm";
        List<Map<String,Object>> kqxmData = DbTools.getSqlToList(kqxmSql);
        for(Map<String,Object> kqxmMap : kqxmData) {
            hsdwInfoMap.put(kqxmMap.get("id").toString(), Util.null2String(kqxmMap.get("hsdw")));
        }
        //处理分组好的数据
        List<Map<String, String>> cqAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealCqDataWithGroup.values()) {
            String ry = list.get(0).get("ygid").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();

            int num = 0;
            double time = 0;
            //查询考勤项目核算单位：天,小时,分钟,次,即0，1，2，3
//            String kqxmSql = "select * from uf_jcl_kq_kqxm where id = " + kqxm;
//            Map<String,Object> kqxmData = DbTools.getSqlToMap(kqxmSql);
            String accountUnit = Util.null2String(hsdwInfoMap.get(kqxm));
            //汇总区间数据
            for (Map<String, Object> map : list) {
                if (!accountUnit.equals("3")) {
                    num++;
                    double sc = Util.null2String(map.get("sc")).equals("") ? 0 : Double.parseDouble(map.get("sc").toString());
                    time += sc;
                } else {
                    num ++;
                }
            }
            //设置出勤汇总子表数据
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("mainid", mainId);
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));
            if (accountUnit.equals("0")) {
                itemMap.put("lt", String.valueOf(time));
            }
            if (accountUnit.equals("1")) {
                itemMap.put("ls", String.valueOf(time));
            }
            if (accountUnit.equals("2")) {
                itemMap.put("lf", String.valueOf((int) time));
            }
            cqAddList.add(itemMap);
        }
        return cqAddList;
    }

    //汇总补打卡表
    public List<Map<String, String>> getBdkAddList(String mainId, String subCompanyListStr, String startDate, String endDate) {
        String bdkSql = "select a.*, b.fbid, b.bm from uf_jcl_kq_bdkjl a left join uf_jcl_kq_cqjg b on a.bdkry = b.ygid and a.dkrq = b.rq where b.fbid in (" + subCompanyListStr + ") and a.dkrq >= '" + startDate +"'" + " and a.dkrq <= '" + endDate +"'";
        List<Map<String,Object>> bdkDates = DbTools.getSqlToList(bdkSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealBdkDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : bdkDates) {
            //补打卡项目
            if (!Util.null2String(map.get("bdklx")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("bdkry") + "_" + map.get("bdklx");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("bdkry", map.get("bdkry"));
                itemMap.put("xm", map.get("bdklx"));
                if (toDealBdkDataWithGroup.get(groupKey) != null) {
                    toDealBdkDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealBdkDataWithGroup.put(groupKey, list);
                }
            }
        }
        //处理分组好的数据
        List<Map<String, String>> bdkAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealBdkDataWithGroup.values()) {
            String ry = list.get(0).get("bdkry").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();
            int num = list.size();
            //设置出勤汇总子表数据
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("mainid", mainId);
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));

            bdkAddList.add(itemMap);
        }
        return bdkAddList;
    }

    //汇总出勤津贴表
    public List<Map<String, String>> getCqjtAddList(String mainId, String subCompanyListStr, String startDate, String endDate) {
        String cqjtSql = "select a.*, b.fbid, b.bm from uf_jcl_kq_cqjt a left join uf_jcl_kq_cqjg b on a.yg = b.ygid and a.rq = b.rq  where b.fbid in (" + subCompanyListStr + ") and  a.rq >= '" + startDate +"'" + " and a.rq <= '" + endDate+"'";
        List<Map<String,Object>> cqjtDates = DbTools.getSqlToList(cqjtSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealCqjtDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : cqjtDates) {
            //津贴类型
            if (!Util.null2String(map.get("jtlx")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("bdkry") + "_" + map.get("bdklx");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("yg", map.get("yg"));
                itemMap.put("xm", map.get("jtlx"));
                if (toDealCqjtDataWithGroup.get(groupKey) != null) {
                    toDealCqjtDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqjtDataWithGroup.put(groupKey, list);
                }
            }
        }
        //处理分组好的数据
        List<Map<String, String>> cqjtAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealCqjtDataWithGroup.values()) {
            String ry = list.get(0).get("yg").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();
            int num = list.size();
            double time = 0;
            //查询考勤项目核算单位：天,小时,分钟,次,即0，1，2，3
            String kqxmSql = "select * from uf_jcl_kq_kqxm where id = " + kqxm;
            Map<String,Object> kqxmData = DbTools.getSqlToMap(kqxmSql);
            String accountUnit = Util.null2String(kqxmData.get("hsdw"));
            //汇总区间数据
            for (Map<String, Object> map : list) {
                if (!accountUnit.equals("3")) {
                    double sc = Util.null2String(map.get("sc")).equals("") ? 0 : Double.parseDouble(map.get("sc").toString());
                    time += sc;
                }
            }
            //设置出勤汇总子表数据
            Map<String, String> itemMap = new HashMap<>();
            itemMap.put("mainid", mainId);
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));
            if (accountUnit.equals("0")) {
                itemMap.put("lt", String.valueOf(time));
            }
            if (accountUnit.equals("1")) {
                itemMap.put("ls", String.valueOf(time));
            }
            if (accountUnit.equals("2")) {
                itemMap.put("lf", String.valueOf((int) time));
            }
            cqjtAddList.add(itemMap);
        }
        return cqjtAddList;
    }

    //临时汇总出勤结果表
    private List<Map<String, Object>> getTemCqAddList(String startDate, String endDate, String subCompanyIds, String departmentIds, String empIds, String empStatus,
                                                      Map<String, Object> userManageInfo, Map<String, String> kqxmUnitInfo) {
        String cqSql = "select a.* from uf_jcl_kq_cqjg a left join hrmresource b on a.ygid = b.id where a.rq >= '" + startDate +"'" + " and a.rq <= '" + endDate +"'";
        if (!subCompanyIds.equals("")) {
            cqSql = cqSql + " and a.fbid in (" + subCompanyIds + ")";
        }
        if (!departmentIds.equals("")) {
            cqSql = cqSql + " and a.bm in (" + departmentIds + ")";
        }
        if (!empIds.equals("")) {
            cqSql = cqSql + " and a.ygid in (" + empIds + ")";
        }
        if(empStatus.length()>0){
            if (!empStatus.equals("8") && !empStatus.equals("9")) {
                cqSql += " and b.status = "+empStatus+ "";
            }else if (empStatus.equals("8")) {
                cqSql += " and (b.status = 0 or b.status = 1 or b.status = 2 or b.status = 3) ";
            }
        }
        List<Map<String,Object>> cqDates = DbTools.getSqlToList(cqSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealCqDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : cqDates) {
            //20240528需求变更，校验当前登录人是否有权限查看相关数据
            if (!checkViewAuth(userManageInfo, map.get("rq").toString(), map.get("ygid").toString())) {
                continue;
            }
            //出勤状态
            String cqzt = Util.null2String(map.get("cqzt"));
            if (!cqzt.equals("")) {
                //匹配出勤状态对应的考勤项目id
                String kqxmId = cqzt.equals("0") ? "1" : "5";
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + kqxmId;
                Map<String,Object> itemMap = new HashMap<>();
                if ("1".equals(kqxmId) && Util.null2String(kqxmUnitInfo.get("1")).equals(AccountingUnitEnum.HOUR.getKey())) {
                    itemMap.put("fbid", map.get("fbid"));
                    itemMap.put("bm", map.get("bm"));
                    itemMap.put("ygid", map.get("ygid"));
                    itemMap.put("xm", kqxmId);
                    itemMap.put("sc", Util.null2String(map.get("cqsc")));
                } else {
                    itemMap.put("fbid", map.get("fbid"));
                    itemMap.put("bm", map.get("bm"));
                    itemMap.put("ygid", map.get("ygid"));
                    itemMap.put("xm", kqxmId);
                    itemMap.put("sc", "1");
                }

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }
            //日期类型
            String rqlx = Util.null2String(map.get("rqlx"));
            if (!rqlx.equals("")) {
                //匹配日期类型对应的考勤项目id
                String kqxmId = null;
                if (rqlx.equals("0") || rqlx.equals("4")) {
                    kqxmId = "2";
                } else if (rqlx.equals("2") || rqlx.equals("3")) {
                    kqxmId = "3";
                } else if (rqlx.equals("1")) {
                    kqxmId = "4";
                }

                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + kqxmId;
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", kqxmId);
                itemMap.put("sc", "1");

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }
            //项目1
            if (!Util.null2String(map.get("xm1")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm1");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm1"));
                itemMap.put("sc", map.get("sc1"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目2
            if (!Util.null2String(map.get("xm2")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm2");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm2"));
                itemMap.put("sc", map.get("sc2"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目3
            if (!Util.null2String(map.get("xm3")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm3");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm3"));
                itemMap.put("sc", map.get("sc3"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目4
            if (!Util.null2String(map.get("xm4")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm4");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm4"));
                itemMap.put("sc", map.get("sc4"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目5
            if (!Util.null2String(map.get("xm5")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm5");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm5"));
                itemMap.put("sc", map.get("sc5"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目6
            if (!Util.null2String(map.get("xm6")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm6");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm6"));
                itemMap.put("sc", map.get("sc6"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目7
            if (!Util.null2String(map.get("xm7")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm7");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm7"));
                itemMap.put("sc", map.get("sc7"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            } else {
                continue;
            }
            //项目8
            if (!Util.null2String(map.get("xm8")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("ygid") + "_" + map.get("xm8");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("ygid", map.get("ygid"));
                itemMap.put("xm", map.get("xm8"));
                itemMap.put("sc", map.get("sc8"));

                if (toDealCqDataWithGroup.get(groupKey) != null) {
                    toDealCqDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqDataWithGroup.put(groupKey, list);
                }
            }

        }
        //处理分组好的数据
        List<Map<String, Object>> cqAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealCqDataWithGroup.values()) {
            String ry = list.get(0).get("ygid").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();

            int num = 0;
            double time = 0;
            //查询考勤项目核算单位：天,小时,分钟,次,即0，1，2，3
            String kqxmSql = "select * from uf_jcl_kq_kqxm where id = " + kqxm;
            Map<String,Object> kqxmData = DbTools.getSqlToMap(kqxmSql);
            String accountUnit = Util.null2String(kqxmData.get("hsdw"));
            //汇总区间数据
            for (Map<String, Object> map : list) {
                if (!accountUnit.equals("3")) {
                    num++;
                    double sc = Util.null2String(map.get("sc")).equals("") ? 0 : Double.parseDouble(map.get("sc").toString());
                    time += sc;
                } else {
                    num ++;
                }
            }
            //设置出勤汇总子表数据
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));
            if (accountUnit.equals("0")) {
                itemMap.put("lt", String.valueOf(time));
            }
            if (accountUnit.equals("1")) {
                itemMap.put("ls", String.valueOf(time));
            }
            if (accountUnit.equals("2")) {
                itemMap.put("lf", String.valueOf(time));
            }
            cqAddList.add(itemMap);
        }
        return cqAddList;
    }

    private boolean checkViewAuth(Map<String, Object> userManageInfo, String rq, String empId) {
        if (empId.equals(String.valueOf(user.getUID()))) {
            return true;
        }
        boolean groupManageSign = (boolean) userManageInfo.get("groupManageSign");
        if (groupManageSign) {
            if (userManageInfo.get(empId) == null) {
                return false;
            }
            String listType = Util.null2String(userManageInfo.get("listType"));
            if (listType.equals(PersonGroupListTypeEnum.PERSON.getKey())) {
                Map<String, Object> forceTimeItem = (Map<String, Object>) userManageInfo.get(empId);
                String startDate = (String) forceTimeItem.get("startDate");
                String endDate = (String) forceTimeItem.get("endDate");
                return rq.compareTo(startDate) >= 0 && ("".equals(endDate) || rq.compareTo(endDate) <= 0);
            } else if (listType.equals(PersonGroupListTypeEnum.CONDITION.getKey()) || listType.equals(PersonGroupListTypeEnum.SQLCONDITION.getKey())) {
                List<Map<String, Object>> forceTimeItemList = (List<Map<String, Object>>) userManageInfo.get(empId);
                boolean forceTimeAllowSign;
                for (Map<String, Object> forceTimeItem : forceTimeItemList) {
                    String startDate = (String) forceTimeItem.get("startDate");
                    String endDate = (String) forceTimeItem.get("endDate");
                    forceTimeAllowSign = rq.compareTo(startDate) >= 0 && ("".equals(endDate) || rq.compareTo(endDate) <= 0);
                    if (forceTimeAllowSign) {
                        return true;
                    }
                }
            }
        } else {
            List<String> subEmpIdList = (List<String>) userManageInfo.get("subEmpIdList");
            return subEmpIdList.size() > 0 && subEmpIdList.contains(empId);
        }
        return false;
    }

    //临时汇总补打卡表
    private List<Map<String, Object>> getTemBdkAddList(String startDate, String endDate, String subCompanyIds, String departmentIds, String empIds, String empStatus, Map<String, Object> userManageInfo) {
        String bdkSql = "select a.*, b.fbid, b.bm from uf_jcl_kq_bdkjl a " +
                "left join uf_jcl_kq_cqjg b on a.bdkry = b.ygid and a.dkrq = b.rq " +
                "left join hrmresource c on a.bdkry = c.id " +
                "where a.dkrq >= '" + startDate +"'" + " and a.dkrq <= '" + endDate +"'";
        if (!subCompanyIds.equals("")) {
            bdkSql = bdkSql + " and b.fbid in (" + subCompanyIds + ")";
        }
        if (!departmentIds.equals("")) {
            bdkSql = bdkSql + " and b.bm in (" + departmentIds + ")";
        }
        if (!empIds.equals("")) {
            bdkSql = bdkSql + " and a.bdkry in (" + empIds + ")";
        }
        if(empStatus.length()>0){
            if (!empStatus.equals("8") && !empStatus.equals("9")) {
                bdkSql += " and c.status = "+empStatus+ "";
            }else if (empStatus.equals("8")) {
                bdkSql += " and (c.status = 0 or c.status = 1 or c.status = 2 or c.status = 3) ";
            }
        }
        List<Map<String,Object>> bdkDates = DbTools.getSqlToList(bdkSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealBdkDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : bdkDates) {
            //20240528需求变更，校验当前登录人是否有权限查看相关数据
            if (!checkViewAuth(userManageInfo, map.get("dkrq").toString(), map.get("bdkry").toString())) {
                continue;
            }
            //补打卡项目
            if (!Util.null2String(map.get("bdklx")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("bdkry") + "_" + map.get("bdklx");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("bdkry", map.get("bdkry"));
                itemMap.put("xm", map.get("bdklx"));
                if (toDealBdkDataWithGroup.get(groupKey) != null) {
                    toDealBdkDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealBdkDataWithGroup.put(groupKey, list);
                }
            }
        }
        //处理分组好的数据
        List<Map<String, Object>> bdkAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealBdkDataWithGroup.values()) {
            String ry = list.get(0).get("bdkry").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();
            int num = list.size();
            //设置出勤汇总子表数据
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));

            bdkAddList.add(itemMap);
        }
        return bdkAddList;
    }
    //临时汇总出勤津贴表
    private List<Map<String, Object>> getTemCqjtAddList(String startDate, String endDate, String subCompanyIds, String departmentIds, String empIds, String empStatus, Map<String, Object> userManageInfo) {
        String cqjtSql = "select a.*, b.fbid, b.bm from uf_jcl_kq_cqjt a " +
                "left join uf_jcl_kq_cqjg b on a.yg = b.ygid and a.rq = b.rq " +
                "left join hrmresource c on a.yg = c.id " +
                "where a.rq >= '" + startDate +"'" + " and a.rq <= '" + endDate +"'";
        if (!subCompanyIds.equals("")) {
            cqjtSql = cqjtSql + " and b.fbid in (" + subCompanyIds + ")";
        }
        if (!departmentIds.equals("")) {
            cqjtSql = cqjtSql + " and b.bm in (" + departmentIds + ")";
        }
        if (!empIds.equals("")) {
            cqjtSql = cqjtSql + " and a.yg in (" + empIds + ")";
        }
        if(empStatus.length()>0){
            if (!empStatus.equals("8") && !empStatus.equals("9")) {
                cqjtSql += " and c.status = "+empStatus+ "";
            }else if (empStatus.equals("8")) {
                cqjtSql += " and (c.status = 0 or c.status = 1 or c.status = 2 or c.status = 3) ";
            }
        }
        List<Map<String,Object>> cqjtDates = DbTools.getSqlToList(cqjtSql);
        //先根据分部、部门、人员、项目分组
        Map<String, List<Map<String,Object>>> toDealCqjtDataWithGroup = new HashMap<>();
        for (Map<String,Object> map : cqjtDates) {
            //20240528需求变更，校验当前登录人是否有权限查看相关数据
            if (!checkViewAuth(userManageInfo, map.get("rq").toString(), map.get("yg").toString())) {
                continue;
            }
            //津贴类型
            if (!Util.null2String(map.get("jtlx")).equals("")) {
                String groupKey = map.get("fbid") + "_" + map.get("bm") + "_" + map.get("bdkry") + "_" + map.get("bdklx");
                Map<String,Object> itemMap = new HashMap<>();
                itemMap.put("fbid", map.get("fbid"));
                itemMap.put("bm", map.get("bm"));
                itemMap.put("yg", map.get("yg"));
                itemMap.put("xm", map.get("jtlx"));
                if (toDealCqjtDataWithGroup.get(groupKey) != null) {
                    toDealCqjtDataWithGroup.get(groupKey).add(itemMap);
                } else {
                    List<Map<String,Object>> list = new ArrayList<>();
                    list.add(itemMap);
                    toDealCqjtDataWithGroup.put(groupKey, list);
                }
            }
        }
        //处理分组好的数据
        List<Map<String, Object>> cqjtAddList = new ArrayList<>();
        for(List<Map<String,Object>> list : toDealCqjtDataWithGroup.values()) {
            String ry = list.get(0).get("yg").toString();
            String kqxm = list.get(0).get("xm").toString();
            String gsfb = list.get(0).get("fbid").toString();
            String gsbm = list.get(0).get("bm").toString();
            int num = list.size();
            double time = 0;
            //查询考勤项目核算单位：天,小时,分钟,次,即0，1，2，3
            String kqxmSql = "select * from uf_jcl_kq_kqxm where id = " + kqxm;
            Map<String,Object> kqxmData = DbTools.getSqlToMap(kqxmSql);
            String accountUnit = Util.null2String(kqxmData.get("hsdw"));
            //汇总区间数据
            for (Map<String, Object> map : list) {
                if (!accountUnit.equals("3")) {
                    double sc = Util.null2String(map.get("sc")).equals("") ? 0 : Double.parseDouble(map.get("sc").toString());
                    time += sc;
                }
            }
            //设置出勤汇总子表数据
            Map<String, Object> itemMap = new HashMap<>();
            itemMap.put("ry", ry);
            itemMap.put("kqxm", kqxm);
            itemMap.put("gzfb", gsfb);
            itemMap.put("gzbm", gsbm);
            itemMap.put("lc", String.valueOf(num));
            if (accountUnit.equals("0")) {
                itemMap.put("lt", String.valueOf(time));
            }
            if (accountUnit.equals("1")) {
                itemMap.put("ls", String.valueOf(time));
            }
            if (accountUnit.equals("2")) {
                itemMap.put("lf", String.valueOf(time));
            }
            cqjtAddList.add(itemMap);
        }
        return cqjtAddList;
    }
}
